# -*- coding:utf-8 -*-
from socket import socket, AF_INET, SOCK_STREAM
import struct
import sys
import os

sys.path.append(os.path.join(sys.path[0], '../..'))

from csi_data import CSI_Data
from csi_config import read_setting

PORT=int(read_setting('port'))

BUF_SIZE = 2048
client = socket(AF_INET, SOCK_STREAM, 0)
client.connect(('127.0.0.1', PORT))
totalcnt = 0
while True:
    try:
        buf = client.recv(BUF_SIZE)
        if buf is None:
            client.close()
            sys.exit(-2)
        i = struct.unpack('>H', buf[:2])
        if i[0] == len(buf) - 2:  # correct package check - 1
            totalcnt += 1

            if buf[2] == 187:  # correct package check - 2
                dat = CSI_Data(buf[3:])
                print('num = %d rssia = %s' % (totalcnt,dat.rssi_a))



    except struct.error as e:
        print(e)
        client.close()
